#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
响应辅助工具
统一API响应格式
"""

from flask import jsonify
from typing import Any, Optional, Dict


def success_response(data: Any = None, message: str = 'Success', status_code: int = 200):
    """
    成功响应
    
    Args:
        data: 响应数据
        message: 响应消息
        status_code: HTTP状态码
        
    Returns:
        Flask响应对象
    """
    response_data = {
        'success': True,
        'message': message,
        'data': data,
        'timestamp': None  # 可以添加时间戳
    }
    
    response = jsonify(response_data)
    response.status_code = status_code
    return response


def error_response(message: str = 'Error', status_code: int = 400, error_code: Optional[str] = None):
    """
    错误响应
    
    Args:
        message: 错误消息
        status_code: HTTP状态码
        error_code: 错误代码
        
    Returns:
        Flask响应对象
    """
    response_data = {
        'success': False,
        'message': message,
        'error_code': error_code,
        'data': None,
        'timestamp': None
    }
    
    response = jsonify(response_data)
    response.status_code = status_code
    return response


def paginated_response(data: list, page: int, per_page: int, total: int, message: str = 'Success'):
    """
    分页响应
    
    Args:
        data: 数据列表
        page: 当前页码
        per_page: 每页数量
        total: 总数量
        message: 响应消息
        
    Returns:
        Flask响应对象
    """
    response_data = {
        'success': True,
        'message': message,
        'data': data,
        'pagination': {
            'page': page,
            'per_page': per_page,
            'total': total,
            'pages': (total + per_page - 1) // per_page,
            'has_next': page * per_page < total,
            'has_prev': page > 1
        },
        'timestamp': None
    }
    
    return jsonify(response_data)